var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { requestFrame } from "utils-lib-js";
export var defaultOptions = {
    type: "interval",
    autoStop: true
};
var TimerManager = (function () {
    function TimerManager(opts) {
        var _this = this;
        this.__id = 0;
        this.timers = {};
        this.fixString = "#@$&*";
        this.isFrame = function () { return _this.opts.type === "frame"; };
        this.opts = __assign(__assign({}, defaultOptions), opts);
    }
    TimerManager.prototype.add = function (handle, delay) {
        if (delay === void 0) { delay = 0; }
        this.initDelay(delay);
        return this.pushTimer(handle, delay);
    };
    TimerManager.prototype.delete = function (timer) {
        var _a;
        var id = timer.id, delay = timer.delay;
        var timers = this.timers;
        if (delay && ((_a = timers[delay]) === null || _a === void 0 ? void 0 : _a.handles))
            timers[delay].handles = timers[delay].handles.filter(function (it) { return it.id !== id; });
    };
    TimerManager.prototype.clear = function () {
        var _this = this;
        var timers = this.timers;
        Object.keys(timers).forEach(function (d) { return _this.stopTimer(timers[d]); });
        this.timers = {};
    };
    TimerManager.prototype.pushTimer = function (handle, delay) {
        var timers = this.timers;
        var __info = {
            id: this.getId(),
            handle: handle,
            delay: delay,
        };
        timers[delay].handles.push(__info);
        return __info;
    };
    TimerManager.prototype.initDelay = function (delay) {
        var timers = this.timers;
        var timer = timers[delay];
        if (timer)
            return;
        timers[delay] = {
            intervalId: null,
            handles: [],
            delay: delay,
        };
        this.startTimer(timers[delay]);
    };
    TimerManager.prototype.startTimer = function (timer) {
        var _this = this;
        if (timer === null || timer === void 0 ? void 0 : timer.intervalId)
            this.stopTimer(timer);
        timer.intervalId = this.delayHandle(function () {
            _this.autoStopTimer(timer);
            timer.handles.forEach(function (it) { return it.handle(); });
        }, timer.delay);
    };
    TimerManager.prototype.stopTimer = function (timer) {
        var intervalId = timer.intervalId;
        if (this.isFrame())
            intervalId();
        else
            clearInterval(intervalId);
    };
    TimerManager.prototype.autoStopTimer = function (timer) {
        var _a = this, autoStop = _a.opts.autoStop, timers = _a.timers;
        var delay = timer.delay;
        if (autoStop && timer.handles.length <= 0) {
            this.stopTimer(timer);
            timers[delay] = null;
        }
    };
    TimerManager.prototype.delayHandle = function (handle, delay) {
        if (this.isFrame())
            return requestFrame(handle, delay);
        else
            return setInterval(handle, delay);
    };
    TimerManager.prototype.getId = function () {
        return "".concat(this.fixString, "-").concat(++this.__id);
    };
    return TimerManager;
}());
export { TimerManager };
export default TimerManager;
